<?php
/* 
    Project     : 48c6c450f1a4a0cc53d9585dc0fee742
    Created on  : Mar 16, 2013, 11:29:15 PM
    Author      : Truong Khuong - khuongxuantruong@gmail.com
    Description :
        Purpose of the stylesheet follows.
*/
class category_model extends CI_Model {

    function __construct($type=null,$init=false)
    {
        parent::__construct();
        $this->cate_type=$type;
		$this->HieCats=array();
		if($init)$this->HierarchicalCats(0);
		
    }
    function get($cate_id){
        $query=$this->db
                ->where("cate_id",$cate_id)
                ->or_where("cate_alias",$cate_id)
                ->get("categories");
        return $query->row();
    }
    function gets($type=''){
        $query=$this->db->query("
            SELECT 
                    CONCAT(if(b.cate_title is null,'',CONCAT(b.cate_title,'/')), a.cate_title) AS path,
                    IF(b.cate_title IS NULL,'0','10') AS level,
                    ROUND(CONCAT(IF(b.cate_position IS NULL,'',CONCAT(b.cate_position,'.')), (a.cate_position)),2) AS path_position,
                    ROUND(CONCAT(IF(b.cate_parent IS NULL,'',CONCAT(b.cate_parent,'.')), (a.cate_parent)),2) AS path_parent,
                    a.*
            FROM 
                    categories a 
                    LEFT JOIN ( categories b ) ON ( a.cate_parent=b.cate_id )
            WHERE a.cate_status='true' AND a.cate_type='$type'
            ORDER BY path_position , a.cate_insert
            ");
                //->order_by('coalesce(columna,columnb)')
                //->order_by('cat_parent','ASC')
                //->order_by('cat_position','ASC')
                //->order_by('_insert','DESC')
                //->get("categories"); 
        return $query->result();
    }
	function get_by_type($type=''){
		if($type!=null)$this->db->where('cate_type',$type);
        $query=$this->db
			->order_by('cate_parent','ASC')
			->order_by('cate_position','ASC')
			->order_by('cate_insert','DESC')
			->get("categories"); 
        return $query->result();
    }
    function HierarchicalCats($cate_id=0){
        if(!isset($this->cats)){
            $this->cats=$this->get_by_type($this->cate_type);
            foreach ($this->cats as $c){
                $c->path=$c->cate_title;
                $this->HieCats[$c->cate_id]=$c;
            }
        }
        $child=array();
        foreach ($this->cats as $c){
            if($c->cate_parent==$cate_id){
                if(isset($this->HieCats[$cate_id])){
					$this->HieCats[$c->cate_id]->level=$c->level=$this->HieCats[$cate_id]->level+1;
                    $this->HieCats[$c->cate_id]->path=$c->path=$this->HieCats[$cate_id]->path."/".$c->cate_title;
                    $this->HieCats[$c->cate_id]->parents=
                            array_unique(
                                    array_merge($this->HieCats[$cate_id]->parents,
                                            array(
                                                $cate_id
                                            )
                                    )
                            );
                }else{
                    $c->parents=array(0);
                    $c->path=$c->cate_title;
					$c->level=1;
                }
                array_push($child, $c);
                $this->HierarchicalCats($c->cate_id);
            }
        }
        if(isset($this->HieCats[$cate_id])) $this->HieCats[$cate_id]->childs=$child;
    }
    function insert($params){
        $this->db->set('cate_insert', 'NOW()', FALSE);
        @$this->db->insert("categories", $params); 
        @$count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function delete($ID){
        $this->db->set('cate_delete', 'NOW()', FALSE);
        $where=array('cate_id'=>$ID);
        $this->db->where($where);
        $this->db->update("categories"); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function retore($ID){
        $this->db->set('cate_delete', 'NULL', FALSE);
        $where=array('cate_id'=>$ID);
        $this->db->where($where);
        $this->db->update("categories"); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function update($ID,$params){
        $this->db->set('cate_update', 'NOW()', FALSE);
        $this->db->where('cate_id', $ID);
        @$this->db->update("categories", $params); 
        @$count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function proc_one(){
        $strSQL = " CALL getCats(?) ";
        $data = array(
            0
        );
        $query = $this->db->query($strSQL, $this->safe_escape($data));
		$error_number = $this->db->_error_number();
        $error_message = $this->db->_error_message();
        if($error_number!==0){
            $_SESSION['auth_db']['errors'][]=$error_message;
			return null;
        }
        $rs = $query->result();
        return $rs;
    }
}
?>
